﻿using CQIE.JonJack.SplLibraryService._IPublicService;
using CQIE.JonJack.SplLibraryService._IReaderService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace CQIE.JonJack.SplLibraryManagementSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ReaderController : ControllerBase
    {
        private readonly IReaderService readerService;

        public ReaderController(IReaderService readerService)
        {
            this.readerService = readerService;
        }

        public record NInfo(string newInfo);

        [HttpPost]
        [Authorize]
        public ActionResult changePassWord([FromBody]NInfo info)
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(readerService.changePassWord(stuNum,info.newInfo));
        }

        [HttpPost]
        [Authorize]
        public ActionResult changePersonInfo([FromBody] NInfo info)
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(readerService.changePersonInfo(stuNum, info.newInfo));
        }
        [HttpGet]
        [Authorize]
        public ActionResult getPersonInfo()
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(readerService.getUserInfo(stuNum));
        }
    }
}
